import json


def calculate_average_score(jsonl_file_path, score_threshold=2.5):
    total_score = 0
    count = 0   # 总数量
    above_count = 0  # 高于阈值的指令数量

    with open(jsonl_file_path, 'r', encoding='utf-8') as file:
        for line in file:
            try:
                # 解析每一行的JSON数据
                data = json.loads(line)
                # 累加score字段的值
                if 'score' in data:
                    total_score += data['score']
                    if data['score'] > score_threshold:
                        above_count += 1
                    count += 1
            except json.JSONDecodeError:
                print(f"Error decoding JSON from line: {line}")

    if count == 0:
        return None  # 如果没有score字段，则返回None

    # 计算平均分
    average_score = total_score / count
    return average_score, above_count


if __name__ == '__main__':
    jsonl_file_path = '../mydata/kimi_generations_seed01_1000/qa_pairs_with_score.jsonl'
    score_threshold = 2.1
    average_score, above_count = calculate_average_score(jsonl_file_path, score_threshold)
    if average_score is not None:
        print(f"Average score: {average_score}")
        print(f"Above {score_threshold} score: {above_count}")
    else:
        print("No score data found.")
